package com.luoxiang.socialandroid.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.activity.GedtActivity;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.MyDynamic;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MyOperationsAdapter extends RecyclerView.Adapter<MyOperationsAdapter.ViewHolder> {
    private List<MyDynamic> list;
    private int flag;
    private View view;

    public MyOperationsAdapter(List<MyDynamic> list, int flag) {
        this.list = list;
        this.flag = flag;
    }

    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    @NonNull
    @Override
    public MyOperationsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_myoperations, parent, false);
        MyOperationsAdapter.ViewHolder holder = new MyOperationsAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyOperationsAdapter.ViewHolder holder, int position) {
        MyDynamic bean = list.get(position);
        holder.tx_op_title.setText(bean.getTitle());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat day = new SimpleDateFormat("dd");
        SimpleDateFormat month = new SimpleDateFormat("MM");
        try {
            Date parse = sdf.parse(bean.getAduitTime());
            //日
            String dayString = day.format(parse);
            //月
            String monthString = month.format(parse);
            holder.tx_date.setText(dayString);
            Integer moonint = Integer.valueOf(monthString);
            holder.tx_moon.setText(moonint + holder.tx_moon.getContext().getString(R.string.mi_yue));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (flag == 6) {
            holder.tx_state.setVisibility(View.VISIBLE);
        } else {
            holder.tx_state.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击跳转个人动态详情
                Intent intent = new Intent(view.getContext(), GedtActivity.class);
                intent.putExtra("bannerList", (Serializable) bean.getUrl());
                intent.putExtra("userId", bean.getUserId());
                intent.putExtra("dymainId", bean.getId());
                intent.putExtra("title", bean.getTitle());
                intent.putExtra("AduitTime", bean.getAduitTime());
                intent.putExtra("likecount",bean.getLikeCount());
                intent.putExtra("islike",bean.getLike());
                view.getContext().startActivity(intent);

            }
        });

        holder.tx_num.setText(holder.tx_num.getContext().getString(R.string.mi_gong) + bean.getUrl().size() + holder.tx_num.getContext().getString(R.string.mi_zhang));
        if (bean.getAuditStatus() == 1) {
            holder.tx_state.setText(holder.tx_state.getContext().getString(R.string.mi_shenhetongguo));
        } else if (bean.getAuditStatus() == 2) {
            holder.tx_state.setText(R.string.mi_shenheshibai);
        } else if (bean.getAuditStatus() == 0) {
            holder.tx_state.setText(R.string.mi_daishenhe);
        }
        if (bean.getUrl().size() == 1) {
            holder.im_l.setVisibility(View.VISIBLE);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(0))
                    .transform(new CenterCrop())
                    .into(holder.im_l);
        } else if (bean.getUrl().size() == 2) {
            holder.im_m1.setVisibility(View.VISIBLE);
            holder.im_m2.setVisibility(View.VISIBLE);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(0))
                    .transform(new CenterCrop())
                    .into(holder.im_m1);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(1))
                    .transform(new CenterCrop())
                    .into(holder.im_m2);
        } else if (bean.getUrl().size() == 3) {
            holder.im_s1.setVisibility(View.VISIBLE);
            holder.im_s2.setVisibility(View.VISIBLE);
            holder.im_s3.setVisibility(View.VISIBLE);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(0))
                    .transform(new CenterCrop())
                    .into(holder.im_s1);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(1))
                    .transform(new CenterCrop())
                    .into(holder.im_s2);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(2))
                    .transform(new CenterCrop())
                    .into(holder.im_s3);
        } else if (bean.getUrl().size() >= 4) {
            holder.im_s1.setVisibility(View.VISIBLE);
            holder.im_s2.setVisibility(View.VISIBLE);
            holder.im_s3.setVisibility(View.VISIBLE);
            holder.im_s4.setVisibility(View.VISIBLE);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(0))
                    .transform(new CenterCrop())
                    .into(holder.im_s1);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(1))
                    .transform(new CenterCrop())
                    .into(holder.im_s2);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(2))
                    .transform(new CenterCrop())
                    .into(holder.im_s3);
            Glide.with(MyAppliaction.getContext())
                    .load(url + bean.getUrl().get(3))
                    .transform(new CenterCrop())
                    .into(holder.im_s4);
        } else {

        }
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tx_date, tx_moon, tx_op_title, tx_num, tx_state;
        private ImageView im_l, im_m1, im_m2, im_s1, im_s2, im_s3, im_s4;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tx_date = itemView.findViewById(R.id.tx_date);
            tx_moon = itemView.findViewById(R.id.tx_moon);
            tx_op_title = itemView.findViewById(R.id.tx_op_title);
            tx_num = itemView.findViewById(R.id.tx_num);
            tx_state = itemView.findViewById(R.id.tx_state);
            im_l = itemView.findViewById(R.id.im_l);
            im_m1 = itemView.findViewById(R.id.im_m1);
            im_m2 = itemView.findViewById(R.id.im_m2);
            im_s1 = itemView.findViewById(R.id.im_s1);
            im_s2 = itemView.findViewById(R.id.im_s2);
            im_s3 = itemView.findViewById(R.id.im_s3);
            im_s4 = itemView.findViewById(R.id.im_s4);
        }
    }
}
